package util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;
import java.io.IOException;

import dao.User;

public class RequestProcessorEx extends RequestProcessor
{
	protected boolean processRoles( HttpServletRequest request,
								HttpServletResponse response,
								ActionMapping mapping )
							throws IOException, ServletException
	{
		String[] roles = mapping.getRoleNames();

		if( roles!=null && roles.length>0 )
		{
			User user = (User)request.getSession().getAttribute("SESSION_USER");
			if(null==user)
			{
				//response.sendRedirect(request.getContextPath()+"/login.jsp" ); 
				//sendRedirect
				RequestDispatcher rd=request.getRequestDispatcher("/error.jsp");
				request.setAttribute("message", "未登录");
				rd.forward(request, response);
				return false;
			}
		}
		return true;
	}
	
	protected boolean processPreprocess(HttpServletRequest request,HttpServletResponse response){
		try{
		   request.setCharacterEncoding("UTF-8");
		  }
		 catch(Exception e){
		   System.out.println("set character failed!!");
		 }
		  return true;
	}
}

